﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace QD.WX.Utill
{
    public class Util
    {

        /// <summary>
        /// 移除所有HTML标签
        /// </summary>
        /// <param name="text">要移除HTML标签的字符串</param>
        public static string RemoveHtml(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<img[^>]*>;", "", RegexOptions.IgnoreCase);
            Htmlstring = Htmlstring.Replace("<", "");
            Htmlstring = Htmlstring.Replace(">", "");
            Htmlstring = Htmlstring.Replace("\r\n", "");
            return Htmlstring;
        }

        public static string RemoveRN(string html)
        {
            html = html.Replace("\r", "");
            html = html.Replace("\n", "");
            html = html.Replace("\r\n", "");
            return html;
        }

        /// <summary>
        /// URL编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlEncode(string str)
        {
            return HttpUtility.UrlEncode(str);
        }
        /// <summary>
        /// URL解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlDecode(string str)
        {
            return HttpUtility.UrlDecode(str);
        }

        public static string MD5str(string strText)
        {
            return MD5str(strText, 32);
        }
        /// <summary>   
        /// 对字符串加密   
        /// </summary>   
        /// <param name="strText">要加密的字符</param>   
        /// <param name="len">是进行16 位还是32位加密</param>   
        /// <returns>返回加密后的字符</returns>   
        public static string MD5str(string strText, int len)
        {
            if (len == 16) //16位MD5加密（取32位加密的9~25字符）  
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strText, "MD5").ToLower().Substring(8, 16);
            }
            if (len == 32) //32位加密  
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strText, "MD5").ToLower();
            }
            return "";
        }

        /// <summary>
        /// 获取系统时间在一年当中是第几周
        /// </summary>
        /// <returns></returns>
        public static int GetWeekOfYear()
        {
            System.Globalization.GregorianCalendar gc = new System.Globalization.GregorianCalendar();
            return gc.GetWeekOfYear(DateTime.Now, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
        }

        public static bool IsUrl(string html)
        {
            Regex r1 = new Regex(@"((http|https)://)?(www.)?[a-z0-9\.]+(\.(com|net|cn|com|org|\.cn|com\.net|net\.cn))(/[^\s\n]*)?");
            return r1.IsMatch(html);
        }

        public static string FilterSql(string sql)
        {
            sql = sql.ToLower().Trim();
            sql = sql.Replace("'", "''");
            sql = sql.Replace("exec", "");
            sql = sql.Replace("delete", "");
            sql = sql.Replace("master", "");
            sql = sql.Replace("truncate", "");
            sql = sql.Replace("declare", "");
            sql = sql.Replace("create", "");
            sql = sql.Replace("xp_", "no");
            return sql;
        }
    }
}
